package de.dailab.graphworld.ontology;

import de.dailab.jiactng.agentcore.comm.ICommunicationAddress;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * A wrapper for the task and possible candidates for its performance.
 *
 * @author Kseniya
 */
public class Job implements Iterable<ICommunicationAddress> {

    private final Task task;
    protected final Map<ICommunicationAddress, Proposal> candidates;

    public Job(Task task) {
        this.task = task;
        this.candidates = new LinkedHashMap<>();
    }

    /**
     * Get the value of task
     *
     * @return the value of task
     */
    public Task getTask() {
        return task;
    }

    @Override
    public Iterator<ICommunicationAddress> iterator() {
        return candidates.keySet().iterator();
    }

    public final Set<ICommunicationAddress> getAllCandidates() {
        return new HashSet<>(candidates.keySet());
    }

    public final void addProposal(ICommunicationAddress candidate, Proposal proposal) {
        // ensure only correct proposals are being stored here
        if (!task.equals(proposal.getTask())) {
            throw new RuntimeException("Incorrect task in added proposal!");
        }
        candidates.put(candidate, proposal);
    }

    public final Proposal getProposalByAssignee(ICommunicationAddress who) {
        return candidates.get(who);
    }

    @Override
    public String toString() {
        return "job " + task;
    }
}